package com.zzyl.service.impl;

import com.zzyl.dto.LoginDto;
import com.zzyl.enums.BasicEnum;
import com.zzyl.exception.BaseException;
import com.zzyl.mapper.LoginServiceMapper;
import com.zzyl.service.LoginService;
import com.zzyl.utils.JwtUtil;
import com.zzyl.vo.UserVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service
public class LoginServiceImpl implements LoginService {
    @Autowired
    private LoginServiceMapper loginServiceMapper;

    //登录
    @Override
    public UserVo login(LoginDto loginDto) {
        UserVo userVo = loginServiceMapper.login(loginDto.getUsername(), loginDto.getPassword());
        if (userVo != null) {
            Map<String, Object> claims = new HashMap<>();
            claims.put("username", userVo.getUsername());
            claims.put("id", userVo.getId());
            String itheima = JwtUtil.createJWT("itheima", 600000, claims);
            userVo.setUserToken(itheima);
            return userVo;
        } else {
            throw new BaseException(BasicEnum.LOGIN_FAIL);
        }
    }


}
